<script setup>
import { ref } from 'vue'
import {articleAddChannelsService,articleEditChannelsService} from '@/api/article'
const dialogVisible = ref(false)
const formModel = ref({
    cate_name:'',
    cate_alias:''
});
//配置规则
const rules = {
    cate_name:[
        {required:true,message:'输入分类名称',trigger:'blur'},
        {pattern:/^\S{1,10}$/,message:'分类名长度不得大于10',trigger:'blur'}
    ],
    cate_alias:[
        {required:true,message:'输入分类名称',trigger:'blur'},
        {pattern:/^[a-zA-Z0-9]{1,15}$/,message:'别名需为15位大小写字母，数字',trigger:'blur'}
    ]
}
const formRef = ref()
const emit = defineEmits(['success'])
//修改数据
const submit = async ()=>{
    await formRef.value.validate()
    const isEdit = formModel.value.id
    if(isEdit){
        await articleEditChannelsService(formModel.value)
        ElMessage.success('编辑成功')
    }else{
        await articleAddChannelsService(formModel.value)
        ElMessage.success('添加成功')
    }
    //关闭弹窗
    dialogVisible.value = false
    //传递成功参数
    emit('success')
}


const open = (row)=>{
    dialogVisible.value = true
    formModel.value = {...row}
}
defineExpose({
    open
})
</script>

<template>
    
    <el-dialog v-model="dialogVisible" :modal="false" :title="formModel.id ? '编辑:':'添加：'" width="500px">
    <el-form ref="formRef" :model="formModel" :rules="rules">
        <el-form-item label="分类名称" prop="cate_name">
            <el-input placeholder="输入分类名称" v-model="formModel.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名" prop="cate_alias">
            <el-input placeholder="输入分类别名" v-model="formModel.cate_alias"></el-input>
        </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">
          确认
        </el-button>
      </div>
    </template>
    </el-dialog>

</template>